package com.youchuang.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

public class MySessionListener implements HttpSessionListener {

	private long onlineCount;
	private Logger logger = Logger.getLogger(MySessionListener.class.getSimpleName());

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		Integer count = (Integer) se.getSession().getServletContext().getAttribute("AccessCount");
		this.onlineCount = this.onlineCount + 1;
		// 保存在application作用域
		se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);

		count = count + 1;
		se.getSession().getServletContext().setAttribute("AccessCount", count);
		logger.info("在线人数：" + onlineCount);

	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		this.onlineCount = this.onlineCount - 1;
		se.getSession().getServletContext().setAttribute("onlineCount", onlineCount);
		logger.info("在线人数：" + onlineCount);
	}

}
